package cn.mcch.investigator.fragment;

import android.content.Intent;
import android.view.View;

import cn.mcch.investigator.R;
import cn.mcch.investigator.activity.LoginActivity;
import cn.mcch.investigator.dialog.LoadingDialog;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.impl.UpdatePasswordModelImpl;
import cn.mcch.investigator.observer.InvestigatorObserverable;
import cn.mcch.investigator.presenter.impl.UpdatePasswordPresenterImpl;
import cn.mcch.investigator.utils.DialogUtils;
import cn.mcch.investigator.utils.ToastUtils;
import cn.mcch.investigator.viewdelegate.impl.UpdatePasswordViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/16.
 */
public class UpdatePasswordFragment extends BaseFragment<UpdatePasswordViewDelegateImpl, UpdatePasswordModelImpl, UpdatePasswordPresenterImpl> implements OnResponseListener {


    private LoadingDialog loadingDialog;

    public UpdatePasswordFragment() {
        super();
        isLoadData = true;
    }

    @Override
    public Class<UpdatePasswordPresenterImpl> getPresenterClass() {
        return UpdatePasswordPresenterImpl.class;
    }

    @Override
    public void createFirstView() {
        mPresenter.createFirstView(getActivity().getApplicationContext(), R.layout.fragment_update_password, this);
    }

    @Override
    public void onClick(View v) {
        if (mPresenter.verify()) {
            loadingDialog = DialogUtils.showLoadingDialog(getFragmentManager(), "正在修改密码，请稍后...");
            mPresenter.updatePassword(1, this);
        }
    }

    @Override
    public void onBefore(int id) {

    }

    @Override
    public void onAfter(boolean result, int id) {
        if (id == 1) {
            // 修改密码
            loadingDialog.dismiss();
            loadingDialog = null;
        }

    }

    @Override
    public void onSuccess(String json, int id) {
        switch (id) {
            case 1:
                // 修改密码成功跳转到登录界面，重新登录
                ToastUtils.showToast("密码修改成功，请重新登录");
                FragmentFactory.clear();
                InvestigatorObserverable.clearAllObservers();
                getActivity().startActivity(new Intent(getActivity().getApplicationContext(), LoginActivity.class));
                // 关闭主页面
                getActivity().finish();
                break;
            default:
                break;
        }
    }
}
